//
//  DUVideoCell.m
//  APP
//
//  Created by duyujin1 on 2021/9/7.
//

#import "DUVideoCell.h"
#import "Masonry.h"

@interface DUVideoCell()
@property(nonatomic,strong) AVPlayerViewController * playerController;
@property(nonatomic,strong) AVPlayer * player;
@property(nonatomic,strong) AVAudioSession * session;
@property (nonatomic,strong)UIImageView * coverView;
@property (nonatomic,copy) NSString * videoUrl;
@property (nonatomic,strong)UIImageView * playButton;
@property (nonatomic,strong)UIView * titleView;
@property (nonatomic,strong)UILabel * titleLabel;
@end

@implementation DUVideoCell

-(instancetype)initWithFrame:(CGRect)frame {
    self = [super initWithFrame:frame];{
        self.layer.shadowOffset = CGSizeMake(0, 0);
        self.layer.shadowColor = [UIColor blackColor].CGColor;
        self.layer.shadowOpacity = 0.6;
        self.layer.shadowRadius = 10;
        self.layer.masksToBounds=NO;
        [self addSubview:self.coverView];
        [self addSubview:self.titleView];
        [self.coverView addSubview:self.playButton];
        [self.titleView addSubview:self.titleLabel];
        
        [self.coverView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.right.top.mas_equalTo(self);
            make.bottom.mas_equalTo(self).offset(-DUVideoToolbarHeight);
        }];
        [self.playButton mas_makeConstraints:^(MASConstraintMaker *make) {
            make.center.mas_equalTo(self.coverView);
            make.height.mas_equalTo(50);
            make.width.mas_equalTo(50);
        }];
        [self.titleView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.mas_equalTo(self.coverView.mas_bottom);
            make.left.right.mas_equalTo(self.coverView);
            make.height.mas_equalTo(DUVideoToolbarHeight);
        }];
        [self.titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
            make.centerY.mas_equalTo(self.titleView);
            make.left.mas_equalTo(self.titleView).offset(10);
        }];
        _session = [AVAudioSession sharedInstance];
        [_session setCategory:AVAudioSessionCategoryPlayback error:nil];
        _player = [AVPlayer playerWithURL:[NSURL URLWithString:@"http://clips.vorwaerts-gmbh.de/big_buck_bunny.mp4"]];
        _playerController = [[AVPlayerViewController alloc] init];
        _playerController.player = _player;
        _playerController.videoGravity = AVLayerVideoGravityResizeAspect;
        _playerController.allowsPictureInPicturePlayback = true;    //画中画，iPad可用
        _playerController.showsPlaybackControls = true;

        //[self addChildViewController:_playerController];
        _playerController.view.translatesAutoresizingMaskIntoConstraints = true;    //AVPlayerViewController 内部可能是用约束写的，这句可以禁用自动约束，消除报错
        _playerController.view.frame = CGRectMake(0, 0, 320, 300);
        //[self.view addSubview:_playerController.view];
        //[_playerController.player play];    //自动播放
    }
    return self;
}



-(void)layoutWithVideoCoverUrl:(NSString *)videoCoverUrl videoUrl:(NSString *)videoUrl{
    _coverView.image=[UIImage imageNamed:videoCoverUrl];
    _playButton.image=[UIImage imageNamed:@"videoPlay"];
    _videoUrl=videoUrl;
}

-(UIImageView *)coverView{
    if(!_coverView){
        _coverView=[[UIImageView alloc] initWithFrame:CGRectZero];
    }
    return _coverView;
}

-(UIImageView *)playButton{
    if(!_playButton){
        _playButton=[[UIImageView alloc] initWithFrame:CGRectZero];
    }
    return _playButton;
}

-(UIView *)titleView{
    if(!_titleView){
        _titleView=[[UIView alloc] initWithFrame:CGRectZero];
        _titleView.backgroundColor=[UIColor whiteColor];
    }
    return _titleView;
}

-(UILabel *)titleLabel{
    if(!_titleLabel){
        _titleLabel=[[UILabel alloc] initWithFrame:CGRectZero];
        _titleLabel.text=@"Big Buck Bunny";
        _titleLabel.textColor=[UIColor blackColor];
        _titleLabel.font=[UIFont systemFontOfSize:20];
    }
    return _titleLabel;
}





@end
